<template>
  <div class="addService">
    <el-dialog
      title="添加业务线"
      :visible.sync="addDialogVisible"
      width="40%"
      :center="true"
      :before-close="addServiceCancel"
    >
      <el-form
        label-width="140px"
        :model="serviceList"
        :rules="rules"
        ref="serviceList"
      >
        <el-form-item label="业务线名称" prop="name">
          <el-input placeholder="请输入" v-model="serviceList.name"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="userId">
          <el-select
            v-model="serviceList.userId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in nameList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addServiceCancel">取 消</el-button>
        <el-button type="primary" @click="addService">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// 引入接口文件
import { principal } from "../../../api/service";

export default {
  name: "AddService",
  props: ["addDialogVisible"],
  data() {
    return {
      nameList: [],
      restaurants: [],
      serviceList: { name: "", userId: "" },
      rules: {
        name: [
          { required: true, message: "请输入业务线名称", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "请输入负责人姓名", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getprincipal();
  },
  methods: {
    // 获取负责人数据
    getprincipal() {
      principal().then((res) => {
        this.nameList = res.data;
      });
    },
    // 取消添加按钮
    addServiceCancel() {
      this.$emit("addServiceCancel", false);
      this.serviceList = { name: "", userId: "" };
      this.$message("已取消新增");
    },
    // 确认添加按钮
    addService() {
      if (this.serviceList.name == "" || this.serviceList.userId == "") {
        this.$message({
          message: "请填写必填项",
          type: "warning",
        });
      } else {
        this.$emit("addService", this.serviceList, false);
        this.serviceList = { name: "", userId: "" };
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.el-input {
  width: 300px;
}
.el-autocomplete {
  width: 300px;
}
</style>
